//
//  StringCompress.swift
//  AlgorithmOne
//
//  Created by 刘彬彬 on 2020/3/16.
//  Copyright © 2020 LBB. All rights reserved.
//

import Foundation

class Compress {
    func solution(_ str:String) -> String {
        
        if str.count <= 1 {
            return str
        }
        
        var targetString = ""
        var count = 0
        var singleString:Character = str.first!
        for s in str {
            if singleString == s {
                count += 1
            } else {
                targetString = targetString+String(singleString)+String(count)
                singleString = s
                count = 1
            }
        }
        targetString = targetString+String(singleString)+String(count)
       
        return targetString.count < str.count ? targetString : str
    }
}
